#include"c3pom/Portal.h"
#include"c3pom/dynclass.h"
#include<iostream>
#include<omp.h>
using namespace std;

//Portal Block部分
Portal_Block::Portal_Block()
{
}
bool Portal_Block::isreceved()
{
	return _data["isreceved"].castto<bool>();
}

Object& Portal_Block::operator[](string name)
{
	return _data[name];
}
Object Portal_Block::getdata(string name)
{
	return _data[name];
}
//====

//Portal部分
Portal::Portal()
{
	attribute["null"]=true;
}

bool Portal::isnull()
{
	return attribute["null"].castto<bool>();
}

Portal::Portal(string name)
{
	attribute["null"]=false;
	cout<<"pbclass:"<<name<<endl;
	Object o=(Portal_Buf*)DYN_CREATE(name.c_str());//根据提供的portal_buf_class来创建portal_buf
	attribute["Portal_Buf"]=o;
	cout<<"Portal_Buf is "<<attribute["Portal_Buf"].gettypename()<<endl;
	omp_init_lock(&lock);

	printf("address 1:%d\n",&attribute);
	printf("address x:%d\n",&this->attribute);
	//test Portal_Buf	
	//Portal_Block pb;
	//pb["test"]=*(new string("what the hell is going on"));
	//attribute["Portal_Buf"].castto<Portal_Buf*>()->push(pb);
	//Portal_Block pbk=attribute["Portal_Buf"].castto<Portal_Buf*>()->pop();
	//cout<<"test data:"<<pbk["test"].castto<string>()<<endl;
	//test end

}

Object Portal::getattr(string name)
{
	return attribute[name];
}

void Portal::push(Portal_Block data)
{
	try{
		omp_set_lock(&lock);
		Object o=attribute["Portal_Buf"];
		printf("address 2:%d\n",&attribute);
		printf("address xx;%d\n",&this->attribute);
		if(!o.isnull())
		{
			Portal_Buf* pb=o.castto<Portal_Buf*>();
			pb->push(data);
			cout<<"push ok"<<endl;
		 }
		else
		{ 
			cout<<"Portal_Buf is null"<<endl;
		}
		omp_unset_lock(&lock);
	} 
	catch(...)
	{  
		return;//发生错误可能是因为Portal_Buf没有设置正确,或者是使用的是不正确的类型
		//log("ERRO:No Portal_Buf set or Portal_Buf is set in a wrong type");
	} 
}

Portal_Block& Portal::pop()
{
	try {
		omp_set_lock(&lock);
		cout<<"read data from Portal"<<endl;
		Object o=attribute["Portal_Buf"];
		Portal_Block pbk;
		if(!o.isnull())
		 {
			cout<<"not null portal_buf"<<endl;
			Portal_Buf* pb=o.castto<Portal_Buf*>();
			cout<<"cast success"<<endl;
			pbk=pb->pop();
		}
		else
		{ 
			cout<<"null portal_buf"<<endl;
		}  
		omp_unset_lock(&lock);
		return pbk;
	}   
	catch(...)
	{
		//log("ERRO:no Portal_Buf set or Portal_Buf is set in a wrong type);
	}
}

void Portal::operator<<(Portal_Block p)
{
	push(p);
}

void Portal::operator>>(Portal_Block& p)
{
	p=pop();
}
void Portal::operator=(Portal p)
{
	attribute=p.attribute;
}
//=======
//
